776D - The Door Problem - CodeForces Solution


2-sat dfs and similar dsu graphs *2000

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
 
using namespace std;
 
#define MOD 1000000007
#define mod(x, m) (((x % m) + m) % m)
#define mxn 200005
#define mxm 100005
#define f first
#define s second
#define pb push_back
#define es " "
#define endl "\n"
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define fastio ios_base::sync_with_stdio(false), cin.tie(nullptr)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> pip;
 
int n, m, v[mxn], viz[mxn][2], memo[mxn], comp[mxn];
vector<int> vec[mxn], vec2[mxn], st;

void dfs(int x){
    memo[x]=1;
    for(int i:vec[x]){
        if(!memo[i]) dfs(i);
    }
    st.pb(x);
}
void dfs2(int x, int c){
    comp[x]=c;
    for(int i:vec2[x]){
        if(!comp[i]) dfs2(i, c);
    }
}

void add(int x, int sx, int y, int sy){
    vec[x+m*sx].pb(y+m*(!sy));
    vec[y+m*sy].pb(x+m*(!sx));

    vec2[y+m*(!sy)].pb(x+m*sx);
    vec2[x+m*(!sx)].pb(y+m*sy);
}

int main(){
    fastio;
    cin >> n >> m;
    for(int i=1; i<=n; i++){
        cin >> v[i];
    }
    for(int i=1; i<=m; i++){
        int k;
        cin >> k;
        for(int j=0; j<k; j++){
            int x;
            cin >> x;
            if(!viz[x][0]) viz[x][0]=i;
            else viz[x][1]=i;
        }
    }
    for(int i=1; i<=n; i++){
        if(v[i]){
            add(viz[i][0], 1, viz[i][1], 0);
            add(viz[i][0], 0, viz[i][1], 1);
        }else{
            add(viz[i][0], 1, viz[i][1], 1);
            add(viz[i][0], 0, viz[i][1], 0);
        }
    }
    for(int i=1; i<=2*m; i++){
        if(!memo[i]) dfs(i);
    }
    int cont=0;
    for(int i=2*m-1; i>=0; i--){
        if(!comp[st[i]]){
            dfs2(st[i], ++cont);
        }
    }
    
    for(int i=1; i<=m; i++){
        if(comp[i]==comp[i+m]){
            cout << "NO" << endl;
            return 0;
        }
    }
    cout << "YES" << endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths